Page({
  data: {
    modalBtn: [
      {
        key: 'change',
        text: '更换',
        type: 'danger',
        plain: true
      }, {
        key: 'confirm',
        text: '确定',
        type: 'danger'
      }
    ],
    statusCode: require('../../config/statusCode').order
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
      curUnix: wx.time.curUnix()
    }, () => {
      // 订单详情
      this.getDetail();
      // 快递信息
      wx.util.request('order.express', {
        query: `order_id=${this.data.id}`
      }).then(express => {
        this.setData({
          express: express
        });
      }).catch(err => {
        console.log(err);
      });
    });
  },
  onShow: function () {
    // 默认发票
    wx.util.request('invoice.default').then(def => {
      this.setData({
        invoice: def
      });
    });
  },
  getDetail: function () {
    wx.util.request('', {
      query: `id=${this.data.id}`
    }).then(order => {
      order.order_money = wx.util.formatAmount(order.order_money);
      order.pay_money = wx.util.formatAmount(order.pay_money);
      order.created_at = wx.time.toDate(order.created_at);
      order.pay_time = order.pay_time > 0 ? wx.time.toDate(order.pay_time) : '';
      order.consign_time = order.consign_time > 0 ? wx.time.toDate(order.consign_time) : '';
      this.setData({
        order: order
      });
    }).catch(err => {
      console.log(err);
    });
  },
  payment: function () {
    wx.navigateTo({
      url: `../payment/index?id=${this.data.id}`
    })
  },
  delivered: function (e) {
    wx.showModal({
      title: '提醒',
      content: '请确认已收到货物，否则有可能财物两空',
      success(u) {
        if (u.confirm) {
          wx.util.request('order.delivery', {
            query: `id=${e.currentTarget.dataset.id}`
          }).then(() => {
            this.setData({
              [`order.order_status`]: 4
            });
          });
        }
      }
    });
  },
  close: function () {
    wx.util.request('order.cancel', {
      query: `id=${this.data.id}`
    }).then(() => {
      this.setData({
        [`order.order_status`]: -4
      });
    });
  },
  del: function () {
    wx.util.toast('功能完善中');
    /*wx.util.request('order.del', {
      query: `id=${this.data.id}`
    }).then(() => {
      wx.navigateBack();
    });*/
  },
  showModal: function () {
    this.setData({
      modalShow: true
    });
  },
  invoice: function (e) {
    if (e.detail.key == 'change') {
      wx.navigateTo({
        url: '../invoice/index?goBack=1',
      })
    } else {
      wx.util.request('invoice.repair', {
        body: {
          order_id: this.data.id,
          invoice_id: this.data.invoice.id,
          invoice_content: '明细'
        }
      }).then(() => {
        this.getDetail();
      });
      this.hideModal();
    }
  },
  hideModal: function () {
    this.setData({
      modalShow: false
    });
  },
  refund: function () {
    wx.navigateTo({
      url: `../refund/apply?id=${this.data.id}`,
    });
  }
})